<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\admin;

use app\mill\builder\Admin;
use app\mill\builder\ZBuilder;
use app\mill\model\Client as ClientModel;
use app\user\model\UserMill as UserMillModel;
use app\user\model\UserGroup as UserGroupModel;
use app\mill\model\Archives as ArchivesModel;
use app\mill\model\Order as OrderModel;
use app\mill\model\Give as GiveModel;
use app\mill\model\Stat as StatModel;
use think\Db;

/**
 * 统计管理控制器
 * @package app\admin\controller
 */
class Stat extends Admin
{

    //初始化
    protected function initialize(){
        parent::initialize();
        GiveModel::init();
    }

    //-------------------------------------------------------- 各工艺统计 --------------------------------------------------
    //首页
    public function index(){


        $url = url('mill/stat/ajaxClient');
        $top_html = StatModel::topStatSpeedHtml();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('') // 页面标题
            ->setTableName('mill_client') // 设置表名
            ->setExtraHtml($top_html,'block_top')
            ->fetch(); // 渲染模板
    }
    public function indexStatHtml(){


        //css
        $css = "<style>
                .top {
                    height: 210px;
                    align-items: stretch;
                    margin-top: 8px;
                    margin-bottom: 3px;
                }
                .top .all {
                    flex: 0 0 440px;
                }
                .top .charts {
                    width: 100%;
                    height: 100%;
                }
                .top .chartBox {
                    width: calc(100% - 500px);
                }
                .tableBox {
                    height: calc(100% - 308px);
                }
                .top .charts-li {
                    width: calc(100% - 519px);
                        height: 158px;
                }
                .top .chartBox .charts1 {
                    flex: 0 0 33.3333%;
                    height: 50%;
                }
            </style>";

        $url = url('mill/stat/ajaxStatSpeed');
        $top_html = StatModel::topStatSpeedHtml($url);

        $data['html'] = $top_html;
        $data['css'] = $css;

        return $data;
    }
    //各工艺统计
    public function ajaxStatSpeed(){

        //下单统计
        $all = Db::name('mill_order_archives')->field('sum(number) as number_sum,sum(row_area) as row_area_sum')->find();
        $all['title'] = '设计下单';
        $all['j_number_sum'] = $all['number_sum'];
        $all['j_row_area_sum'] = $all['row_area_sum'];

        //各工艺统计
        //获取生产状态字段
        $give_sort = GiveModel::$give_status_search;
        unset($give_sort[0]);

        $list = [];
        foreach ($give_sort as $gk=>$gv){
            if($gk == 0){
                $sort_arr = $all;
                $sort_arr['title'] = '设计下单';
            }
            else{
                $sort_arr = Db::name('mill_order_archives_give_log')->field('count(1) as number_sum,sum(one_area) as row_area_sum')->where(['status'=>$gk])->find();
                $sort_arr['title'] = $gv;
                $sort_arr['j_number_sum'] = $sort_arr['number_sum'];
                $sort_arr['j_row_area_sum'] = $sort_arr['row_area_sum'];
            }
            $list[] = $sort_arr;
        }
        $data['all'] = $all;
        $data['list'] = $list;
        echo json_encode(['data' => $data],JSON_UNESCAPED_UNICODE);

    }
    //各工艺统计
    public function speed($give_status = ''){
        $top = $this->indexStatHtml();

        // 获取查询条件
        $map = $this->getMap();
        dump($map);exit;
        $map2 = '';
        if(!empty($give_status)){
            $map2 = "find_in_set({$give_status},give_sort)";
        }

        //上一个环节的扫码情况
        $give_status_old = ($give_status-1);

        // 数据列表
        $data_list = Db::name('mill_order')
            ->alias('o')
            ->field("o.*,{$give_status} as speed_give_status,{$give_status_old} as give_status_old")
            ->where($map)
            ->where($map2)
            ->order('o.point_time desc,o.archives_bill_time desc')
            ->paginate()
            ->each(function ($v){

                //$v['create_time'] = date('Y-m-d',$v['create_time']);
                //$v['company'] = $v['company'].ClientModel::getTags($v);
                //$v['sales_mobile'] = '<font color="#68FF84">'.$v['sales_mobile'].'</font>';
                //$v['note'] = '<font color="#DA0000">'.$v['note'].'</font>';

                //上一个环节的扫码情况
                $give_status_old = $v['give_status_old'];
                $where = [
                    ['order_id','=',$v['id']],
                    ['status','=',$give_status_old],
                ];
                if(in_array($give_status_old,explode(',',$v['give_sort']))){
                    $old_log = Db::name('mill_order_archives_give_log')->field('count(id) as status_sum_old,sum(one_area) as status_area_sum_old')->where($where)->find();
                }
                else $old_log['status_sum_old'] = $old_log['status_area_sum_old'] = '<font class="text-danger">无此工艺</font>';

                //这个环节的当天扫码情况
                $strt_time = strtotime(date('Y-m-d'));
                $end_time = strtotime(date('Y-m-d 23:59:59'));
                $where = [
                    ['order_id','=',$v['id']],
                    ['status','=',$v['speed_give_status']],
                    ['status_time','BETWEEN',[$strt_time,$end_time]],
                ];
                $day_log = Db::name('mill_order_archives_give_log')->field('count(id) as status_sum_day,sum(one_area) as status_area_sum_day')->where($where)->find();

                //这个环节总的扫码情况
                $where = [
                    ['order_id','=',$v['id']],
                    ['status','=',$v['speed_give_status']],
                ];
                $log = Db::name('mill_order_archives_give_log')->field('count(id) as status_sum,sum(one_area) as status_area_sum,min(status_time) as start_time,max(status_time) as end_time')->where($where)->find();
                //有效周期
                $log['give_pro_valid'] = Db::name('mill_order_archives_give_log')->field("DATE_FORMAT(FROM_UNIXTIME(status_time), '%Y-%m-%d') as status_time")->where($where)->group("DATE_FORMAT(FROM_UNIXTIME(status_time), '%Y-%m-%d')")->select();
                $log['give_pro_valid'] = count($log['give_pro_valid']);
                if(empty($log['give_pro_valid']))$log['give_pro_valid'] = '';
                else $log['give_pro_valid'] = '<font class="text-warning">'.count($log['give_pro_valid']).'(天)</font>';

                $log['status_no_sum'] = floatval($v['archives_number_sum']) - floatval($log['status_sum']);
                $log['status_area_no_sum'] = floatval($v['archives_area_sum']) - floatval($log['status_area_sum']);

                $v = array_merge($v,$old_log,$day_log,$log);

                //周期
                $v['give_pro'] = timeToday($v['start_time'],$v['end_time']);
                if(!empty($v['give_pro']))$v['give_pro'] = '<font class="text-warning">'.$v['give_pro'].'(天)</font>';

                //完成状态
                $v['give_status_str'] = '<font class="text-danger">未完成</font>';
                if(!empty($v['archives_number_sum']) && empty($v['status_no_sum']))$v['give_status_str'] = '<font class="text-success">已完成</font>';

                //订单状态
                $v['status_str'] = '正在生产';
                if(empty($v['is_lock']))$v['status_str'] = '<font class="text-danger">冻结</font>';

                return $v;
            })
        ;

        $give_status_old_str = GiveModel::$give_status_search[$give_status_old];
        $give_status_str = GiveModel::$give_status_search[$give_status];
        $title = "{$give_status_str}统计";

        $columns1 = [
            ['status_sum_old', '完成数量(件)','','','','badge-orange',['group'=>"{$give_status_old_str}数据"]],
            ['status_no_sum_old', '未完成数量(件)','','','','badge-orange',['group'=>"{$give_status_old_str}数据"]],
            ['status_sum_day', '日产数量(件)','','','','badge-warning',['group'=>"{$give_status_str}日产数据"]],
            ['status_area_sum_day', '日产面积(㎡)','','','','badge-warning',['group'=>"{$give_status_str}日产数据"]],
            ['status_sum', '总完成数(件)','','','','badge-primary',['group'=>"{$give_status_str}生产数据"]],
            ['status_no_sum', '未完成数(件)','','','','badge-primary',['group'=>"{$give_status_str}生产数据"]],
            ['status_area_sum', '总完成面积(㎡)','','','','badge-primary',['group'=>"{$give_status_str}生产数据"]],
            ['status_area_no_sum', '未完成面积(㎡)','','','','badge-primary',['group'=>"{$give_status_str}生产数据"]],
        ];

        if(empty($give_status) || $give_status == 1){
            unset($columns1[0]);
            unset($columns1[1]);
        }

        $columns = [ // 批量添加列
            ['point_time', '交货日期','date','','','badge-green',['group'=>'工程信息']],
            ['archives_bill_time', '下单日期','date','','','badge-green',['group'=>'工程信息']],
            ['engineering', '工程名称','','','','badge-green',['group'=>'工程信息']],
            ['archives_sn', '档案号','','','','badge-green',['group'=>'工程信息']],
            ['type', '订单类型','','',OrderModel::$types,'badge-green',['group'=>'工程信息']],
            ['batch', '批次','','','','badge-green',['group'=>'工程信息']],
            ['archives_area_sum', '总面积(m²)','','','','badge-green',['group'=>'工程信息']],
            ['archives_number_sum', '总数量(件)','','','','badge-green',['group'=>'工程信息']],
        ];

        $columns2 = [
            ['give_status_str', '完成状态','','','','badge-orange',['group'=>'状态详情']],
            ['status_str', '订单状态','','','','badge-orange',['group'=>'状态详情']],
            ['give_pro', '周期','','','','badge-warning',['group'=>'生产周期']],
            ['give_pro_valid', '有效周期','','','','badge-warning',['group'=>'生产周期']],
        ];

        $columns = array_merge($columns,$columns1,$columns2);

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle($title) // 页面标题
            ->setTableName('mill_order') // 设置表名
            ->setExtraHtml($top['html'],'block_top')
            ->setExtraCss($top['css'])
            //->hideCheckbox()
            ->setSearch(['o.engineering' => '工程名称', 'l.archives_sn' => '档案号']) // 设置搜索参数
            ->setSearchArea([
                ['select:2', 'o.type', '订单类型','','',OrderModel::$types],
                ['select:2', 'o.mill_id', '车间选择','','',ClientModel::getMillList()],
                ['daterange:2', 'c.create_time', '日期'],
            ])
            ->addColumns($columns)
            ->addTopButton('print',['title' => '导出列表','icon'=>'','class'=>'btn btn-success quality-print','float'=>'right','lay-event'=>'quality-print'])
            ->setRowList($data_list) // 设置表格数据
            ->setColumnWidth([
                'engineering'  => 160,
            ])
            ->fetch(); // 渲染模板
    }

    public function speed1(){
        return $this->speed(1);
    }
    public function speed2(){
        return $this->speed(2);
    }
    public function speed3(){
        return $this->speed(3);
    }
    public function speed4(){
        return $this->speed(4);
    }
    public function speed5(){
        return $this->speed(5);
    }
    public function speed6(){
        return $this->speed(6);
    }
    //------------------------------------------------------ 各工艺统计 END ------------------------------------------------

    //----------------------------------------------------- 客户各类信息展示 -----------------------------------------------

    //客户阶段统计
    public function ajaxClient()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['create_time','BETWEEN',[$start_time,$end_time]]
            ];
            $stat['title'] = "{$i}月";
            $stat['value'] = Db::name('mill_client')->where($where)->count();

            $rank[$stat['title']] = $stat['value'];
            $head[] = $stat['title'];
            $data[] = $stat['value'];
        }

        //按值从高到低排
        arsort($rank);

        echo json_encode(['head' => $head,'data' => $data,'rank' => $rank],JSON_UNESCAPED_UNICODE);
    }

    //客户订单信息
    public function clientOrder(){

        $client_id = input('client_id');

        $where = ['c.id'=>$client_id];
        // 数据列表
        $data = Db::name('mill_client')
            ->alias('c')
            ->field('c.*,ss.staff as back,s.staff as sales,s.staff_mobile as sales_mobile,count(o.id) as order_sum,sum(a.row_area) as area_sum,sum(a.number) as number_sum')
            ->leftJoin('mill_order o','o.client_id=c.id')
            ->leftJoin('mill_staff s','c.sales_id=s.id')
            ->leftJoin('mill_staff ss','c.back_id=ss.id')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->where($where)
            ->find();

        $data['create_time'] = date('Y-m-d',$data['create_time']);
        $data['area_sum'] = round($data['area_sum'],4)." (M²)";
        $data['number_sum'] = floatval($data['number_sum'])." (件)";
        $data['order_sum'] = "总计 {$data['order_sum']} 个";

        $columns = [
            ['field'=>'sales','title'=>'业务经理'],
            ['field'=>'back','title'=>'内 勤'],
            ['field'=>'create_time','title'=>'创建时间'],
            ['field'=>'order_sum','title'=>'工程数量'],
            ['field'=>'area_sum','title'=>'总计面积'],
            ['field'=>'number_sum','title'=>'总计件数'],
        ];

        exit(json_encode(['title'=>$data['company'],'columns'=>$columns,'row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }

    //客户总工程列表
    public function orderList(){

        $client_id = input('client_id');

        $where = ['c.id'=>$client_id];
        // 数据列表
        $list = Db::name('mill_order')
            ->alias('o')
            ->field('o.*,count(o.id) as order_sum')
            ->leftJoin('mill_client c','o.client_id=c.id')
            ->where($where)
            ->group('o.order_pid')
            ->select();

        //获取发货部的产品状态
        $address_status = GiveModel::$last_give_status;

        if(!empty($list)){
            foreach ($list as &$v){

                $archvices = ArchivesModel::getOneStat($v['order_pid'],'order_pid','=',[],[],$address_status);
                $v = array_merge($v,$archvices);
            }
        }

        $columns = [
            ['field'=>'order_pid','title'=>'序号'],
            ['field'=>'engineering','title'=>'工程名称'],
            ['field'=>'order_sum','title'=>'批次数量(批次)'],
            ['field'=>'number_sum','title'=>'总数量(件)'],
            ['field'=>'area_sum','title'=>'总面积(㎡)'],
            ['field'=>'status_sum','title'=>'发货完成面积(㎡)'],
            ['field'=>'status_area_sum','title'=>'发货完成数量(件)'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }

    //客户工程工艺核算
    public function orderSpeed(){

        $client_id = input('client_id');
        $order_pid = input('order_pid');
        $order_id = input('order_id');

        $where = [];
        if($client_id)$where['l.client_id'] = $client_id;
        if($order_pid)$where['l.order_pid'] = $order_pid;
        if($order_pid)$where['l.order_id'] = $order_id;
        // 数据列表
        $list = Db::name('mill_order_archives_give_log')
            ->alias('l')
            ->field('l.status,l.mill_id,count(l.id) as number_sum,sum(l.one_area) as area_sum,min(status_time) as start_time,max(status_time) as end_time')
            ->where($where)
            ->group('l.status,l.mill_id')
            ->order('l.status asc,l.mill_id asc')
            ->select();

        //获取车间
        $mill_list = ClientModel::getMillList();
        //工艺选择
        $give_status = GiveModel::$give_status_search;

        if(!empty($list)){
            foreach ($list as &$v){

                //周期
                $v['give_pro'] = timeToday($v['start_time'],$v['end_time']);

                //有效周期
                $v['give_pro_valid'] = Db::name('mill_order_archives_give_log')->field("DATE_FORMAT(FROM_UNIXTIME(status_time), '%Y-%m-%d') as status_time")->where($where)->where(['status'=>$v['status']])->group("DATE_FORMAT(FROM_UNIXTIME(status_time), '%Y-%m-%d')")->select();
                $v['give_pro_valid'] = count($v['give_pro_valid']);
                if(empty($v['give_pro_valid']))$v['give_pro_valid'] = '';

                $v['mill_name'] = isset($mill_list[$v['mill_id']])?$mill_list[$v['mill_id']]:'暂无车间';
                $v['status_str'] = isset($give_status[$v['status']])?$give_status[$v['status']]:'该工艺已解除';

            }
        }

        $columns = [
            ['field'=>'','title'=>'序号','type'=>'numbers'],
            ['field'=>'mill_name','title'=>'车间'],
            ['field'=>'status_str','title'=>'工艺环节'],
            ['field'=>'area_sum','title'=>'完成平方(KG)'],
            ['field'=>'number_sum','title'=>'完成数量(件)'],
            ['field'=>'give_pro','title'=>'生产周期(天)'],
            ['field'=>'give_pro_valid','title'=>'有效周期(天)'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }

    //客户工程物料核算
    public function paintColist(){

        $client_id = input('client_id');
        $order_pid = input('order_pid');
        $order_id = input('order_id');

        $where = [];
        if($client_id)$where['l.client_id'] = $client_id;
        if($order_pid)$where['l.order_pid'] = $order_pid;
        if($order_pid)$where['l.order_id'] = $order_id;
        // 数据列表
        $list = [];

        $columns = [
            ['field'=>'','title'=>'序号','type'=>'numbers'],
            ['field'=>'mill_name','title'=>'物料名称'],
            ['field'=>'status_str','title'=>'实际用量(KG)'],
            ['field'=>'area_sum','title'=>'喷涂面积/开平重量'],
            ['field'=>'number_sum','title'=>'成本核算(金额)'],
            ['field'=>'give_pro','title'=>'覆盖率(kg/㎡)'],
            ['field'=>'give_pro2','title'=>'平方率(㎡/kg)'],
        ];
        //dump($list);exit;

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }

    //客户工程质量管理汇总
    public function qualityList(){

        $client_id = input('client_id');
        $order_pid = input('order_pid');
        $order_id = input('order_id');

        $where = [];
        if($client_id)$where['l.client_id'] = $client_id;
        if($order_pid)$where['l.order_pid'] = $order_pid;
        if($order_pid)$where['l.order_id'] = $order_id;
        // 数据列表
        $list = Db::name('mill_order_archives_give_quality')
            ->alias('l')
            ->field('l.client_id,l.status,l.quality_errors,l.mill_id,count(l.id) as number_sum,sum(l.one_area) as area_sum')
            ->where($where)
            ->group('l.quality_errors,l.mill_id')
            ->order('l.quality_errors asc,l.mill_id asc')
            ->select();

        //获取车间
        $mill_list = ClientModel::getMillList();

        if(!empty($list)){
            foreach ($list as &$v){

                $v['mill_name'] = isset($mill_list[$v['mill_id']])?$mill_list[$v['mill_id']]:'暂无车间';

                $where = ['mill_id'=>$v['mill_id'],'status'=>abs($v['status'])];
                if($client_id)$where['client_id'] = $client_id;
                if($order_pid)$where['order_pid'] = $order_pid;

                $v['give_sum'] = Db::name('mill_order_archives_give_log')->where($where)->count();

                if($v['give_sum'] > 0)$v['give_rate'] = (1-round(($v['number_sum']/$v['give_sum']),2))*100;
                else $v['give_rate'] = 0;
                $v['give_rate'] = $v['give_rate'].'%';

            }
        }

        $columns = [
            ['field'=>'','title'=>'序号','type'=>'numbers'],
            ['field'=>'mill_name','title'=>'车间'],
            ['field'=>'quality_errors','title'=>'错误工序'],
            ['field'=>'area_sum','title'=>'错误面积(㎡)'],
            ['field'=>'number_sum','title'=>'错误数量(件)'],
            ['field'=>'give_rate','title'=>'合格率(%)'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }

    //--------------------------------------------------- 客户各类信息展示 END ---------------------------------------------

    //----------------------------------------------------- 工程各类信息展示 -----------------------------------------------

    //工程阶段统计
    public function ajaxOrder()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['create_time','BETWEEN',[$start_time,$end_time]]
            ];
            $stat['title'] = "{$i}月";
            $stat['value'] = Db::name('mill_order')->where($where)->count();

            $rank[$stat['title']] = $stat['value'];
            $head[] = $stat['title'];
            $data[] = $stat['value'];
        }

        //按值从高到低排
        arsort($rank);

        echo json_encode(['head' => $head,'data' => $data,'rank' => $rank],JSON_UNESCAPED_UNICODE);
    }

    //工程阶段统计，按车间统计
    public function ajaxOrder2()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        //获取生产车间
        $mill_list = ClientModel::getMillList();

        $series = [];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $head[] = "{$i}月";

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['create_time','BETWEEN',[$start_time,$end_time]]
            ];

            //再细分生产车间统计
            foreach ($mill_list as $mk=>$mv){

                if(!isset($series[$mk]))$series[$mk] = ['name'=>$mv,'type'=>'bar','data'=>[]];

                $series[$mk]['data'][] = Db::name('mill_order')->where($where)->where(['mill_id'=>$mk])->count();

            }

        }
        $series = array_values($series);

        echo json_encode(['head' => $head,'series' => $series],JSON_UNESCAPED_UNICODE);
    }

    //工程数据汇总
    public function orderBatch(){

        $order_pid = input('order_pid');

        $where = ['o.order_pid'=>$order_pid];
        // 数据列表
        $data = Db::name('mill_order')
            ->alias('o')
            ->field('o.*,ss.staff as back,s.staff as sales,s.staff_mobile as sales_mobile,count(o.id) as order_sum,sum(a.row_area) as area_sum,sum(a.number) as number_sum')
            ->leftJoin('mill_staff s','o.sales_id=s.id')
            ->leftJoin('mill_staff ss','o.back_id=ss.id')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->where($where)
            ->find();

        $data['create_time'] = date('Y-m-d',$data['create_time']);
        $data['area_sum'] = round($data['area_sum'],4)." (M²)";
        $data['number_sum'] = floatval($data['number_sum'])." (件)";
        $data['order_sum'] = "总计 {$data['order_sum']} 个";

        $columns = [
            ['field'=>'sales','title'=>'业务经理'],
            ['field'=>'back','title'=>'内 勤'],
            ['field'=>'create_time','title'=>'创建时间'],
            ['field'=>'order_sum','title'=>'工程数量'],
            ['field'=>'area_sum','title'=>'总计面积'],
        ];

        exit(json_encode(['title'=>$data['engineering'],'columns'=>$columns,'row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }

    //工程批次列表
    public function batchList(){

        $order_pid = input('order_pid');

        $where = ['o.order_pid'=>$order_pid];
        // 数据列表
        $list = Db::name('mill_order')
            ->alias('o')
            ->field('o.*')
            ->where($where)
            ->order('o.id desc')
            ->select();

        $columns = [
            ['field'=>'id','title'=>'序号'],
            ['field'=>'archives_sn','title'=>'档案号'],
        ];

        //获取生产状态字段
        $give_status = GiveModel::$give_status_search;
        unset($give_status[0]);
        foreach ($give_status as $gsk=>$gsv){
            $columns[] = ['field'=>"status{$gsk}_mill_name",'title'=>$gsv,'width'=>'140'];
        }

        //获取发货部的产品状态
        $address_status = GiveModel::$last_give_status;

        //获取生产车间
        $mill_list = ClientModel::getMillList();

        if(!empty($list)){
            foreach ($list as &$v){

                $archvices = ArchivesModel::getOneStat($v['id'],'order_id','=',[],[],$address_status);
                $v = array_merge($v,$archvices);

                //获取各环节参与的生产车间
                foreach ($give_status as $gsk=>$gsv){
                    $mill_arr = Db::name('mill_order_archives_give_log')->where(['order_id'=>$v['id'],'status'=>$gsk])->group('mill_id')->column("mill_id");
                    $mill_str = '';
                    foreach ($mill_arr as $mill_id){
                        if($mill_id == 0)continue;
                        $mill_str .= isset($mill_list[$mill_id])?'<font style="color: #FFA800;margin: 0 5px;">'.$mill_list[$mill_id].'</font>':'';
                    }
                    $v["status{$gsk}_mill_name"] = empty($mill_str)?'':$mill_str;

                }

            }
        }

        $columns2 = [
            ['field'=>'batch','title'=>'批次'],
            ['field'=>'number_sum','title'=>'总数量(件)'],
            ['field'=>'area_sum','title'=>'总面积(㎡)'],
            ['field'=>'status_sum','title'=>'发货完成面积(㎡)','width'=>'90'],
            ['field'=>'status_area_sum','title'=>'发货完成数量(件)','width'=>'90'],
        ];
        $columns = array_merge($columns,$columns2);

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }

    //--------------------------------------------------- 工程各类信息展示 END ---------------------------------------------

    //----------------------------------------------------- 批次各类信息展示 -----------------------------------------------
    //批次统计
    public function ajaxBatch()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['create_time','BETWEEN',[$start_time,$end_time]]
            ];
            $stat['title'] = "{$i}月";
            $stat['value'] = Db::name('mill_order')->where($where)->count();

            $rank[$stat['title']] = $stat['value'];
            $head[] = $stat['title'];
            $data[] = $stat['value'];
        }

        //按值从高到低排
        arsort($rank);

        echo json_encode(['head' => $head,'data' => $data,'rank' => $rank],JSON_UNESCAPED_UNICODE);
    }
    //批次阶段统计
    public function ajaxBatchSpeed()
    {

        $order_id = input('order_id');

        //获取生产状态字段
        $give_status = GiveModel::$give_status_search;
        unset($give_status[0]);

        //获取工程信息
        $order = Db::name('mill_order')->where(['id'=>$order_id])->find();
        $order['number_sum'] = Db::name('mill_order_archives')->where(['order_id'=>$order_id])->sum('number');
        //获取当前工程安排的工序
        $give_sort = explode(',',$order['give_sort']);

        //获取当天时间
        $start_time = strtotime(date('Y-m-d'));
        $end_time = ($start_time+86400)-1;

        //组装数据
        $day_speed = ['title'=>'当天产量','list'=>[]];
        $all_speed = ['title'=>'生产周期','list'=>[]];
        foreach ($give_sort as $gs){
            if(!isset($give_status[$gs]))continue;

            //工艺名称
            $v['give_title'] = $give_status[$gs];

            $where = [['order_id','=',$order_id],['status','=',$gs],];
            //------------------------------------------生产周期--------------------------------------------
            //扫码件数
            $give_log = Db::name('mill_order_archives_give_log')->field('count(id) as number_sum,min(status_time) as start_time,max(status_time) as end_time')->where($where)->find();
            $v = array_merge($v,$give_log);

            $v['give_pro'] = timeToday($v['start_time'],$v['end_time']);

            //完成状态
            $v['status_str'] = '';
            if(!empty($order['number_sum'])){
                if($v['number_sum'] == $order['number_sum'])$v['status_str'] = '已完成';
                else $v['status_str'] = "预估完成 {$v['give_pro']}天";
            }
            $v['number_sum'] .= '(件)';

            //生产周期
            $all_speed['list'][] = $v;

            //------------------------------------------当天产量--------------------------------------------
            $where[] = ['status_time','BETWEEN',[$start_time,$end_time]];
            //扫码件数
            $give_log = Db::name('mill_order_archives_give_log')->field('count(id) as number_sum,min(status_time) as start_time,max(status_time) as end_time')->where($where)->find();
            $v = array_merge($v,$give_log);

            $v['give_pro'] = timeToday($v['start_time'],$v['end_time']);

            //完成状态
            if($v['status_str'] != '已完成' && !empty($order['number_sum'])){
                if($v['number_sum'] == $order['number_sum'])$v['status_str'] = '已完成';
                else $v['status_str'] = "预估完成 {$v['give_pro']}天";
            }
            $v['number_sum'] .= '(件)';

            //生产周期
            $day_speed['list'][] = $v;

        }

        $data = [$day_speed,$all_speed];

        exit(json_encode(['title'=>'','row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }
    //批次数据汇总
    public function orderBatch2(){

        $order_id = input('order_id');
        $archives_id = input('archives_id');

        $where = ['o.id'=>$order_id];
        // 数据列表
        $data = Db::name('mill_order')
            ->alias('o')
            ->field('o.*,c.company,ss.staff as back,s.staff as sales,s.staff_mobile as sales_mobile,count(o.id) as order_sum,sum(a.row_area) as area_sum,sum(a.number) as number_sum')
            ->leftJoin('mill_client c','o.client_id=c.id')
            ->leftJoin('mill_staff s','o.sales_id=s.id')
            ->leftJoin('mill_staff ss','o.back_id=ss.id')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->where($where)
            ->find();

        $data['archives_sn'] = $data['archives_sn'].ClientModel::getTags($data);
        $data['archives_status_time'] = date('Y.m.d',$data['archives_status_time']);
        $data['archives_sign_time'] = date('Y.m.d',$data['archives_sign_time']);
        $data['area_sum'] = round($data['area_sum'],4)." (M²)";
        $data['number_sum'] = floatval($data['number_sum'])." (件)";
        $data['order_sum'] = "总计 {$data['order_sum']} 个";

        $columns = [
            ['field'=>'company','title'=>''],
            ['field'=>'engineering','title'=>''],
            ['field'=>'sales','title'=>'业务经理'],
            ['field'=>'back','title'=>'内 勤'],
            ['field'=>'archives_status_time','title'=>'设计审核日期'],
            ['field'=>'archives_sign_time','title'=>'图纸确认日期'],
            ['field'=>'area_sum','title'=>'总计面积'],
            ['field'=>'number_sum','title'=>'总计件数'],
        ];

        exit(json_encode(['title'=>$data['archives_sn'],'columns'=>$columns,'row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }
    //批次产品列表
    public function giveList()
    {

        $order_id = input('order_id');

        $where = ['g.order_id'=>$order_id];

        /*// 数据列表
        $list = Db::name('mill_order_archives_give_log')
            ->alias('g')
            ->field("g.*,GROUP_CONCAT(g.status) as group_status,GROUP_CONCAT(g.status_uid) as group_status_uid,GROUP_CONCAT(g.status_time) as group_status_time,a.one_area,a.model,a.model_sn,a.thickness,a.color,a.sort as a_sort")
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($where)
            ->group('g.archives_id')
            ->order('a.sort asc')
            ->select();*/
        // 数据列表
        $list = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field("g.*,a.one_area,a.model,a.model_sn,a.thickness,a.color,a.sort as a_sort")
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($where)
            ->group('g.id')
            ->order('a.sort asc')
            ->select();

        $user_list = ClientModel::getStaffList(3);

        $give_sort = GiveModel::$give_status_search;
        // 删除第一个元素
        unset($give_sort[0]);
        // 删除最后一个元素
        array_pop($give_sort);

        if(!empty($list)){
            foreach ($list as &$v){

                //获取扫码记录
                //$log = Db::name('mill_order_archives_give_log')->field('GROUP_CONCAT(status) as group_status,GROUP_CONCAT(status_uid) as group_status_uid,GROUP_CONCAT(status_time) as group_status_time')->where(['give_id'=>$v['id']])->find();
                $log = Db::name('mill_order_archives_give_log')->field('GROUP_CONCAT(CONCAT(status,"|",status_uid,"|",status_time)) as status_str')->where(['give_id'=>$v['id']])->find();

                $log_status_arr = explode(',',$log['status_str']);

                $log_status_arr2 = [];
                if(!empty($log['status_str'])){
                    foreach ($log_status_arr as $la){
                        $la_arr = explode('|',$la);
                        $log_status_arr2[$la_arr[0]] = ['status'=>$la_arr[0],'status_uid'=>$la_arr[1],'status_time'=>$la_arr[2]];
                    }
                }

                //遍历各状态扫码人
                foreach ($give_sort as $gk=>$gv){

                    $v["status{$gk}_str"] = "";
                    if(!empty($log_status_arr2[$gk])){
                        $log2 = $log_status_arr2[$gk];
                        //扫码人
                        $nickname = isset($user_list[$log2['status_uid']])?$user_list[$log2['status_uid']]:'已禁用';
                        //扫码时间
                        $status_time = date('Y.m.d H:i',$log2['status_time']);

                        $v["status{$gk}_str"] = "({$nickname}){$status_time}";
                    }

                }

                //序号
                $v['a_sort'] = "{$v['a_sort']}-{$v['counter']}";
                //扫码人
                $v['nickname'] = isset($user_list[$v['status_uid']])?$user_list[$v['status_uid']]:'已禁用';
                //扫码时间
                $v['status_time'] = !empty($v['status_time'])?date('Y.m.d H:i',$v['status_time']):'';
                //生产状态
                $v['status_str'] = GiveModel::$give_status[$v['status']];
                if($v['quality_status']<0)$v['status_str'] .= GiveModel::$quality_status[$v['quality_status']];
                elseif ($v['quality_status'] == -3)$v['status_str'] = GiveModel::$quality_status[$v['quality_status']];
                if(!empty($v['status_time']))$v['status_str'] .= "({$v['nickname']}){$v['status_time']}";

            }
        }


        $columns = [
            ['field'=>'sort','title'=>'序号','width'=>40],
            ['field'=>'a_sort','title'=>'产品序号','width'=>70],
        ];

        //遍历各状态设置表头
        foreach ($give_sort as $gk=>$gv){
            $columns[] = ['field'=>"status{$gk}_str",'title'=>$gv,'width'=>160];
        }

        $columns1 = [
            ['field'=>'color','title'=>'颜色'],
            ['field'=>'model_sn','title'=>'铝板编号'],
            ['field'=>'status_str','title'=>'当前工序','width'=>180],
        ];

        $columns = array_merge($columns,$columns1);

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }
    //批次工艺环节核算
    public function batchSpeed2(){

        $order_id = input('order_id');
        $archives_id = input('archives_id');

        //获取生产状态字段
        $give_status = GiveModel::$give_status_search;
        unset($give_status[0]);
        $give_sort = Db::name('mill_order')->where(['id'=>$order_id])->value('give_sort');

        $data = [];
        if(!empty($give_sort)){
            $give_sort = explode(',',$give_sort);
            foreach ($give_sort as $gk){

                $gs = isset($give_status[$gk])?$give_status[$gk]:'该工艺已解除';

                $info = ArchivesModel::getOneStat($archives_id,'archives_id','=',[],[],$gk);

                $info['give_sort'] = $gk;
                $info['title'] = $gs;
                $info['area_sum'] = round($info['area_sum'],4);

                $info['status_str'] = '未完成';
                if($info['number_sum'] > 0 && $info['status_no_sum'] == 0)$info['status_str'] = '已完成';

                $data[] = $info;
            }
        }

        $columns = [
            ['field'=>'give_sort','title'=>'序号'],
            ['field'=>'title','title'=>'工艺环节'],
            ['field'=>'status_sum','title'=>'已完成数量(件)'],
            ['field'=>'status_no_sum','title'=>'未完成数量(件)'],
            ['field'=>'status_area_sum','title'=>'已完成面积(㎡)'],
            ['field'=>'status_area_on_sum','title'=>'未完成面积(㎡)'],
            ['field'=>'status_str','title'=>'完成状态'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }
    //--------------------------------------------------- 批次各类信息展示 END ---------------------------------------------

    //----------------------------------------------------- 产品各类信息展示 -----------------------------------------------
    //工程阶段统计
    public function ajaxSpeedShow()
    {

        $mill_id = input('mill_id');
        $is_day = input('is_day');

        //获取当天时间
        $start_time = strtotime(date('Y-m-d'));
        $end_time = ($start_time+86400)-1;

        //获取生产状态字段
        $give_status = GiveModel::$give_status_search;
        unset($give_status[0]);
        $give_sort = $give_status;
        if(!empty($mill_id)){
            $group_ids = Db::name('admin_user_mill')->where(['id'=>$mill_id])->value('group_ids');
            if(!empty($group_ids))$give_sort = Db::name('admin_user_group')->where([['id','in',$group_ids],['give_sort','>',0],['status','=',1]])->order('give_sort asc')->column('give_title','give_sort');
        }

        $data = [];
        if(!empty($give_sort)){
            foreach ($give_sort as $gk=>$gs){

                $where = [['status','=',$gk]];
                if($is_day)$where[] = ['status_time','BETWEEN',[$start_time,$end_time]];
                elseif($mill_id)$where[] = ['mill_id','=',$mill_id];
                $info = Db::name('mill_order_archives_give_log')->field('count(id) as number_sum,sum(one_area) as area_sum,max(status_time) as status_time')->where($where)->find();

                $info['title'] = $gs;
                $info['area_sum'] = round($info['area_sum'],4);
                $info['status_time'] = $info['status_time']?date('Y.m.d',$info['status_time']):date('Y.m.d');

                $data[] = $info;
            }
        }

        exit(json_encode(['title'=>'','row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }

    //批次生产状态
    public function batchSpeed(){

        $order_id = input('order_id');
        $archives_id = input('archives_id');

        //获取生产状态字段
        $give_status = GiveModel::$give_status_search;
        unset($give_status[0]);
        $give_sort = Db::name('mill_order')->where(['id'=>$order_id])->value('give_sort');

        $data = [];
        if(!empty($give_sort)){
            $give_sort = explode(',',$give_sort);
            foreach ($give_sort as $gk){

                $gs = isset($give_status[$gk])?$give_status[$gk]:'该工艺已解除';

                $info = ArchivesModel::getOneStat($archives_id,'archives_id','=',[],[],$gk);

                $info['give_sort'] = $gk;
                $info['title'] = $gs;
                $info['area_sum'] = round($info['area_sum'],4);

                $info['status_str'] = '未完成';
                if($info['number_sum'] > 0 && $info['status_no_sum'] == 0)$info['status_str'] = '已完成';

                $data[] = $info;
            }
        }

        $columns = [
            ['field'=>'give_sort','title'=>'序号'],
            ['field'=>'title','title'=>'工艺环节'],
            ['field'=>'status_sum','title'=>'已完成数量(件)'],
            ['field'=>'status_no_sum','title'=>'未完成数量(件)'],
            ['field'=>'status_area_sum','title'=>'已完成面积(㎡)'],
            ['field'=>'status_area_on_sum','title'=>'未完成面积(㎡)'],
            ['field'=>'status_str','title'=>'完成状态'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$data],JSON_UNESCAPED_UNICODE));
    }

    //扫码记录
    public function giveLogList()
    {

        $archives_id = input('archives_id');
        $status = input('status');

        $where = ['g.archives_id'=>$archives_id,'g.status'=>$status];

        // 数据列表
        $list = Db::name('mill_order_archives_give_log')
            ->alias('g')
            ->field("g.*,a.one_area,a.model,a.model_sn,a.thickness,a.color,a.sort as a_sort")
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($where)
            ->order('g.status_time desc')
            ->select();

        $user_list = ClientModel::getStaffList(3);

        if(!empty($list)){
            foreach ($list as &$v){

                //序号
                $v['a_sort'] = "{$v['a_sort']}-{$v['counter']}";
                //扫码人
                $v['nickname'] = isset($user_list[$v['status_uid']])?$user_list[$v['status_uid']]:'已禁用';
                //扫码时间
                $v['status_time'] = date('Y.m.d H:i:s',$v['status_time']);
                //生产状态
                $v['status_str'] = '<span class="label label-primary">'.GiveModel::$give_status[$v['status']];
                if($v['quality_status']<0)$v['status_str'] .= GiveModel::$quality_status[$v['quality_status']];
                elseif ($v['quality_status'] == -3)$v['status_str'] = '<span class="label label-primary">'.GiveModel::$quality_status[$v['quality_status']];
                $v['status_str'] .= '</span>';

                //质量判定
                $v['quality_errors'] = Db::name('mill_order_archives_give_quality')->where(['give_id'=>$v['id'],'status'=>"-{$v['status']}"])->value('quality_errors');

                //补扫记录
                $v['is_bu_str'] = '';

                if($v['is_bu']){
                    $v['group_name'] = Db::name('admin_user_group')->where(['id'=>$v['group_id']])->value('name');
                    $v['is_bu_str'] = "{$v['group_name']}[{$v['nickname']}]".$v['status_time'];
                }

            }
        }

        $columns = [
            ['field'=>'a_sort','title'=>'序号'],
            ['field'=>'give_sn','title'=>'二维码编码'],
            ['field'=>'nickname','title'=>'扫码人'],
            ['field'=>'status_time','title'=>'扫码时间'],
            ['field'=>'status_str','title'=>'当前扫码状态'],
            ['field'=>'quality_errors','title'=>'质量判定'],
            ['field'=>'is_bu_str','title'=>'补扫记录'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }

    //扫码记录统计
    public function ajaxMonthGive()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        $series = [
            'number_sum'    =>  ['name'=>'数量(件)','type'=>'line','data'=>[]],
            'area_sum'      =>  ['name'=>'面积(m²)','type'=>'line','data'=>[]],
        ];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['status_time','BETWEEN',[$start_time,$end_time]]
            ];
            $stat['title'] = "{$i}月";
            $archives = Db::name('mill_order_archives_give')->field('count(id) as number_sum,sum(one_area) as area_sum')->where($where)->find();
            $archives['area_sum'] = round($archives['area_sum'],4);
            $stat['value'] = $archives['number_sum'].'___'.$archives['area_sum'];

            $rank[$stat['title']] = $stat['value'];
            $head[] = $stat['title'];
            $data[] = $stat['value'];

            $series['number_sum']['data'][] = $archives['number_sum'];
            $series['area_sum']['data'][] = $archives['area_sum'];

        }

        $series = array_values($series);

        //按值从高到低排
        arsort($rank);

        echo json_encode(['head' => $head,'series' => $series,'rank' => $rank],JSON_UNESCAPED_UNICODE);
    }

    //质检统计
    public function ajaxQuality()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        $series = [
            'number_sum'    =>  ['name'=>'数量(件)','type'=>'line','data'=>[]],
            'area_sum'      =>  ['name'=>'面积(m²)','type'=>'line','data'=>[]],
        ];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['quality_time','BETWEEN',[$start_time,$end_time]]
            ];
            $stat['title'] = "{$i}月";
            $archives = Db::name('mill_order_archives_give_quality')->field('count(id) as number_sum,sum(one_area) as area_sum')->where($where)->find();
            $archives['area_sum'] = round($archives['area_sum'],4);
            $stat['value'] = $archives['number_sum'].'___'.$archives['area_sum'];

            $rank[$stat['title']] = $stat['value'];
            $head[] = $stat['title'];
            $data[] = $stat['value'];

            $series['number_sum']['data'][] = $archives['number_sum'];
            $series['area_sum']['data'][] = $archives['area_sum'];

        }

        $series = array_values($series);

        //按值从高到低排
        arsort($rank);

        echo json_encode(['head' => $head,'series' => $series,'rank' => $rank],JSON_UNESCAPED_UNICODE);
    }

    //物流统计
    public function ajaxAddress()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = $rank = [];

        $series = [
            'number_sum'    =>  ['name'=>'数量(件)','type'=>'bar','data'=>[],'itemStyle'=>['normal'=>['label'=>['show'=>true]]]],
            'area_sum'      =>  ['name'=>'面积(m²)','type'=>'line','data'=>[],'itemStyle'=>['normal'=>['label'=>['show'=>true]]],'smooth'=>true],
        ];

        //按月份统计
        for ($i=1;$i<=12;$i++){

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;
            $where = [
                ['status_time','BETWEEN',[$start_time,$end_time]],
                ['status','=',GiveModel::$last_give_status],
            ];
            $stat['title'] = "{$i}月";
            $archives = Db::name('mill_order_archives_give_log')->field('count(id) as number_sum,sum(one_area) as area_sum')->where($where)->find();
            $archives['area_sum'] = round($archives['area_sum'],4);
            $stat['value'] = $archives['number_sum'].'___'.$archives['area_sum'];

            $rank[$stat['title']] = $stat['value'];
            $head[] = $stat['title'];
            $data[] = $stat['value'];

            $series['number_sum']['data'][] = $archives['number_sum'];
            $series['area_sum']['data'][] = $archives['area_sum'];

        }

        $series = array_values($series);

        //按值从高到低排
        arsort($rank);

        echo json_encode(['head' => $head,'series' => $series,'rank' => $rank],JSON_UNESCAPED_UNICODE);
    }

    //计价统计
    public function ajaxArchivesPrice()
    {

        //年份
        $year = input('year',date('Y'));

        $head = $data = [];

        $series = [
            'address_price'    =>  ['name'=>'发货金额（元）','type'=>'line','data'=>[],'itemStyle'=>['normal'=>['label'=>['show'=>true]]]],
            'initial_price'    =>  ['name'=>'计价金额（元）','type'=>'line','data'=>[],'itemStyle'=>['normal'=>['label'=>['show'=>true]]]],
        ];

        //按月份统计
        for ($i=1;$i<=12;$i++){
            $head[] = "{$i}月";

            $start_time = strtotime("{$year}-{$i}-1");
            $end_time = strtotime("+1 month",$start_time)-1;

            $series['address_price']['data'][] = Db::name('mill_order_archives_address')->where([['create_time','BETWEEN',[$start_time,$end_time]]])->sum('address_price');

            $series['initial_price']['data'][] = Db::name('mill_order')->where([['initial_time','BETWEEN',[$start_time,$end_time]]])->sum('initial_price');

        }

        $series = array_values($series);

        echo json_encode(['head' => $head,'series' => $series],JSON_UNESCAPED_UNICODE);
    }
    //--------------------------------------------------- 产品各类信息展示 END ---------------------------------------------

    //----------------------------------------------------- 发货 -----------------------------------------------------------
    //AJAX发货单列表页
    public function ajaxAddressList()
    {
        //定时任务，超过24小时未确认发货，则自动发货
        //GiveModel::taskSubGiveAddress();

        $keyword = input('keyword');
        $map = [['g.address_sn','<>',""]];
        //if(!empty($keyword))$map[] = ['g.address_sn|g.archives_sn','like',"%{$keyword}%"];
        if(!empty($keyword))$map[] = ['g.archives_sn','like',"%{$keyword}%"];

        // 数据列表
        $list = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field("g.archives_sn,g.address_sn,concat('发货单号：',g.address_sn) as address_sn_str,concat('档案号：',g.archives_sn) as archives_sn_str,'' as right_str")
            ->where($map)
            ->group("g.address_sn")
            ->order("g.address_status asc,g.address_date desc,g.address_sn desc")
            ->select();

        $columns = [
            ['field'=>'address_sn_str','title'=>''],
            ['field'=>'archives_sn_str','title'=>''],
            ['field'=>'right_str','title'=>'','width'=>'20'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }
    //--------------------------------------------------- 发货 END ---------------------------------------------------------

    //----------------------------------------------------- 计价 -----------------------------------------------------------
    //AJAX计价列表页
    public function ajaxArchivesPriceList()
    {
        //定时任务，超过24小时未确认发货，则自动发货
        //GiveModel::taskSubGiveAddress();

        $keyword = input('keyword');
        $map = [['o.archives_status','=',2]];
        if(!empty($keyword))$map[] = ['o.archives_sn|o.engineering','like',"%{$keyword}%"];

        // 数据列表
        $list = Db::name('mill_order')
            ->alias('o')
            ->field("o.*,o.id as order_id,concat('工程名称：',o.engineering) as address_sn_str,concat('档案号：',o.archives_sn) as archives_sn_str,'' as right_str")
            ->where($map)
            ->group('o.id')
            ->order('o.archives_status_time desc,o.id desc')
            ->select();

        $columns = [
            ['field'=>'archives_sn_str','title'=>'','width'=>'130'],
            ['field'=>'address_sn_str','title'=>'','width'=>'auto'],
            ['field'=>'right_str','title'=>'','width'=>'20'],
        ];

        exit(json_encode(['title'=>'','columns'=>$columns,'row_list'=>$list],JSON_UNESCAPED_UNICODE));
    }
    //--------------------------------------------------- 计价 END ---------------------------------------------------------

}
